<?php
// this is another example showing how the dPFramework is working
// additionally we will have an easy database connection here

// as we are now within the tab box, we have to state (call) the needed information saved in the variables of the parent function
GLOBAL $AppUI, $canRead, $canEdit, $m, $tab, $typeUnit;
require_once(DP_BASE_DIR."/modules/masterdata/mdunit.class.php");
if (!$canRead) {			// lock out users that do not have at least readPermission on this module
	$AppUI->redirect( "m=public&a=access_denied" );
}
$active = intval( dPgetParam( $_REQUEST, 'active', 1 ) );
$type =  $_REQUEST['type'];

require_once(DP_BASE_DIR."/modules/masterdata/mdtype.class.php");
$types= arrayMerge(array(''=>$AppUI->_('--Select Type--')), getTypeList($typeUnit, TRUE));

//prepare an html table with a head section
?>
<table class="infopanel" width="100%" cellspacing="0" cellpadding="7" border="0">
<tr>
	<td colspan="100%"><input type="button" class="button" onClick="doEditUnit(0)" value="<?php echo $AppUI->_( 'New unit' );?>"/></td>
	<td align="right">
		<form name="MdUnitFilter" action="?m=<?=$m?>&tab=<?=$tab?>" method="post">
			<?= arraySelect($types,'type','style="font-size:10px" onchange="this.form.submit()"',$type)?>
			<input  class="button" onclick ="this.form.submit()" type="radio" name="active" value="-1" <?php if ($active == -1) echo "checked" ?> /><?=$AppUI->_('All')?>
			<input  class="button" onclick ="this.form.submit()" type="radio" name="active" value="1" <?php if ($active == 1) echo "checked" ?> /> <?=$AppUI->_('Active') ?>
			<input  class="button" onclick ="this.form.submit()" type="radio" name="active" value="0" <?php if ($active == 0) echo "checked" ?> /> <?=$AppUI->_('Disable')?>
		</form>
	</td>
</tr>
</table>
<table id="idTableMasterdata" class="list" width="100%" cellspacing="0" cellpadding="2" border="0">
<tbody>
<tr>

	<th nowrap="nowrap">&nbsp;</th>
	<!--th nowrap="nowrap"><?php echo $AppUI->_( 'Code' );?></th-->
	<th nowrap="nowrap"><?php echo $AppUI->_( 'Name' );?></th>
	<th nowrap="nowrap"><?php echo $AppUI->_( 'Status' );?></th>
	<th nowrap="nowrap"><?php echo $AppUI->_( 'Type' );?></th>
	<th nowrap="nowrap"><?php echo $AppUI->_( 'Active' );?></th>
	<th nowrap="nowrap"><?php echo $AppUI->_( 'Delete' );?></th>

</tr>

<?php
// retrieving some dynamic content using an easy database query
// add/show now gradually the einstein quotes
/*
$unitO = new CMdUnit();
$unit = $unitO->loadAllUnit($active);
*/
$q = new DBQuery;
$q->addTable('md_unit');
$q->addJoin('md_status', 'status', 'status_id = unit_status');
$q->addJoin('md_type', 'type', 'type_id = unit_type');
$q->addQuery('md_unit.*, status.status_name, type.name as type_name');
if($active >-1){
	$q->addWhere('unit_active = '.$active);
}
if($type!= null && $type!=''){
	$q->addWhere('unit_type = '.$type);
}
$q->addOrder('unit_name');
$unit = $q->loadList();
if (empty($unit)) {?>
<tr class="second">
<td colspan="100"><?php echo $AppUI->_( 'No item available' );?></td>
</tr>
<?php
}
$i = 0;
foreach ($unit as $row) {		//parse the array of einstein quotes
	$i++;
?>
<tr class="<?=($i % 2 == 0) ? "second" : "first" ?>">
	<td nowrap="nowrap" width="20">


	<?php if ($canEdit) {	// in case of writePermission on the module show an icon providing edit functionality for the given quote item

		// call the edit site with the unique id of the quote item
		//echo "\n".'<a href="./index.php?m=masterdata&a=mdcurrency_edit&currency_id=' . $row["currency_id"] . '">';

		// use the dPFrameWork to show the image
		// always use this way via the framework instead of hard code for the advantage
		// of central improvement of code in case of bugs etc. and for other reasons
		echo "\n".'<a onclick="doEditUnit('.$row["unit_id"]. ')">';
		echo dPshowImage( 'images/edit.gif' );
		echo "\n</a>";
	}
	?>
	</td>
	<!--td> <?php echo $row["unit_code"];?></td-->
	<td><?php echo $row["unit_name"];?></td>
	<td><?php echo $row["status_name"];?></td>
	<td><?php echo $row["type_name"];?></td>
	<td ><?php if($row["unit_active"]==1){ echo dPshowImage( 'images/tick.gif' );}?></td>
	<td>
	<?php if ($canEdit) {
		showIconLink('images/delete.gif', 'js:doDelete('.$row["unit_id"]. ', \''.$row["unit_name"].'\')', 'Delete');
	}
	?>
	</td>
</tr>
<?php
}
?>
</tbody>
</table>
<div id="idUnitDialog" class="popup" style="visibility: hidden; left: 436px; top: 94.5px; display: none;">
</div>

<script type="text/javascript" language="javascript">
	Table("idTableMasterdata").setNormalHover(1);
</script>
<form name="MdFormDelete" id="MdFormDelete" method="post">
	<input type="hidden" name="dosql" value ="mdunit_do_delete" />
	<input type="hidden" name="m" value ="masterdata" />
	<input type="hidden" id="del_cc_id" name="unit_id" value ="" />
	<input type="hidden" name="del" value ="" />
</form>